# -*- coding: utf-8 -*-
# @Time    : 2019/6/11 18:03
# @Author  : Igor
# @File    : test.py
# @Software: PyCharm

from PIL import Image
from PIL import ImageChops


def compare_images(path_one, path_two, diff_save_location):
    """
    比较图片，如果有不同则生成展示不同的图片

    @参数一: path_one: 第一张图片的路径
    @参数二: path_two: 第二张图片的路径
    @参数三: diff_save_location: 不同图的保存路径
    """
    image_one = Image.open(path_one)
    image_two = Image.open(path_two)
    try:
        diff = ImageChops.difference(image_one, image_two)

        if diff.getbbox() is None:
            # 图片间没有任何不同则直接退出
            print("两张图片一致！")
        else:
            diff.save(diff_save_location)
    except Exception as e:
        text = ("图片大小和对应的宽度不一致，使用同样尺寸的图片避免上述问题")
        print("【{0}】{1}".format(e, text))


if __name__ == '__main__':
    compare_images('1.png',
                   '2.png',
                   '比对结果.png')